package androidvn.xenforo.fourums;

import java.util.ArrayList;
import java.util.HashMap;

import java.util.Iterator;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListFragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import androidvn.xenforo.fourums.R;
import androidvn.xenforo.fourums.adapter.CustomNodeListAdapter;
import androidvn.xenforo.fourums.model.Node;

public class ForumNodeFragment extends ListFragment {

	private ArrayList<Node> nodeList, forumNodeList, childList;
	private HashMap<String, ArrayList<Node>> forumChildNodeList;
	Node node = new Node();
	int groupPosition, childPosition;
	String categoryId;
	boolean loginStatus;
	// ALL JSON node names
	private static final String TAG_NODE_ID = "node_id";
	private static final String TAG_TITLE = "title";
	private static final String TAG_PARENT_NODE_ID = "parent_node_id";
	private static final String TAG_DISPLAY_ORDER = "display_order";
	private static final String TAG_DESCRIPTION = "description";
	private static final String TAG_DISPLAY_IN_LIST = "display_in_list";
	private static final String TAG_DEPTH = "depth";

	// private static final String TAG_BOX = "nameBox";

	setOnForumNodeClickListener communicator;

	@Override
	public void onAttach(Activity activity) {

		super.onAttach(activity);
		try {
			communicator = (setOnForumNodeClickListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement setOnForumClickListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.forum_node_list_layout,
				container, false);

		Bundle bundle = this.getArguments();
		if (bundle != null) {
			categoryId = bundle.getString("nodeId");
			groupPosition = bundle.getInt("groupPosition");
			childPosition = bundle.getInt("childPosition");

			Log.d("On Forum Params >>>", "CategoryID: " + categoryId + " / "
					+ groupPosition + " - " + childPosition + " / Status: "
					+ Global.loginStatus + " - Username: " + Global.userName
					+ " - Hash: " + Global.userHash);
		}

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		nodeList = new ArrayList<Node>();
		forumNodeList = new ArrayList<Node>();
		childList = new ArrayList<Node>();
		forumChildNodeList = new HashMap<String, ArrayList<Node>>();

		//
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(getActivity());
		try {
			JSONObject jsonNodesRespond = new JSONObject(sp.getString(
					"jsonNodes", "[]"));
			Node node = null;
			Iterator<?> keys = jsonNodesRespond.keys();
			while (keys.hasNext()) {
				String key = (String) keys.next();
				JSONObject jsonNode = jsonNodesRespond.getJSONObject(key);
				node = new Node();
				node.setNodeId(jsonNode.getString(TAG_NODE_ID));
				node.setTitle(jsonNode.getString(TAG_TITLE));
				node.setParentNodeId(jsonNode.getString(TAG_PARENT_NODE_ID));
				node.setDisplayOrder(jsonNode.getString(TAG_DISPLAY_ORDER));
				node.setDescription(jsonNode.getString(TAG_DESCRIPTION));
				node.setDisplayInList(jsonNode.getString(TAG_DISPLAY_IN_LIST));
				node.setDepth(jsonNode.getString(TAG_DEPTH));

				nodeList.add(node);
			}
			// childList depth 2
			for (int i = 0; i < nodeList.size(); i++) {
				if (nodeList.get(i).getDepth().equals("2")
						&& nodeList.get(i).getDisplayInList().equals("1")) {
					childList.add(nodeList.get(i));
				}
			}

			for (int i = 0; i < nodeList.size(); i++) {
				if (nodeList.get(i).getParentNodeId().equals(categoryId)) {
					forumNodeList.add(nodeList.get(i));
				}
				for (int j = 0; j < forumNodeList.size(); j++) {

					for (int j2 = 0; j2 < childList.size(); j2++) {
						if (forumNodeList.get(j).getNodeId()
								.equals(childList.get(j2).getParentNodeId())) {
							forumChildNodeList.put(forumNodeList.get(j)
									.getNodeId(), childList);
						}
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		CustomNodeListAdapter adapter = new CustomNodeListAdapter(
				getActivity(), forumNodeList);
		setListAdapter(adapter);
		// on forum item click
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// set action title
				getActivity().setTitle(forumNodeList.get(position).getTitle());
				// check it has child or not
				if (forumChildNodeList.get(forumNodeList.get(position)
						.getNodeId()) != null) {
					communicator.onForumNodeClick(forumNodeList.get(position)
							.getNodeId(), true);
				} else {
					communicator.onForumNodeClick(forumNodeList.get(position)
							.getNodeId(), false);
				}

			}
		});
	}

	public void setCommunicator(setOnForumNodeClickListener communicator) {
		this.communicator = communicator;
	}

	interface setOnForumNodeClickListener {
		public void onForumNodeClick(String nodeId, boolean haveChild);
	}
}
